package net.chenlin.dp.modules.business.service.impl;

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import net.chenlin.dp.modules.business.service.MinioSysFileService;
import net.chenlin.dp.modules.conf.MinioConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Minio 文件存储
 *
 * @author ruoyi
 */
@Service
public class MinioSysFileServiceImpl implements MinioSysFileService {

    @Autowired
    private MinioConfig minioConfig;

    @Autowired
    private MinioClient client;

    /**
     * 本地文件上传接口
     *
     * @param file 上传的文件
     * @return 访问地址
     * @throws Exception
     */
    @Override
    public String uploadExcelFile(MultipartFile file) throws Exception {
        //String fileName = file.getOriginalFilename();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = dateFormat.format(new Date());
        PutObjectArgs args = PutObjectArgs.builder()
                .bucket(minioConfig.getBucketName())
                .object(fileName)
                .stream(file.getInputStream(), file.getSize(), -1)
                .contentType(file.getContentType())
                .build();
        client.putObject(args);
        return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
    }

//    /**
//     * 本地文件上传接口
//     *
//     * @param file 上传的文件
//     * @return 访问地址
//     * @throws Exception
//     */
//    @Override
//    public String uploadExcelFile(MultipartFile file) throws Exception {
//        MinioClient client = MinioClient.builder().endpoint("http://139.9.159.254:1003").credentials("yangsm", "yangsm123").build();
//        String fileName = file.getName();
//        //String fileName = FileUploadUtils.extractFilename(file);
//        PutObjectArgs args = PutObjectArgs.builder()
//                .bucket("import-file")
//                .object(fileName)
//                .stream(file.getInputStream(), file.getSize(), -1)
//                .contentType(file.getContentType())
//                .build();
//        client.putObject(args);
//        return "asdfasdf";
//        //return "http://139.9.159.254:1002" + "/" + "import-file" + "/" + fileName;
//    }
}
